/**
 * Renderer for rendering cities as points
 */
Ext.define('okcapital.layer.renderer.CityRenderer', {
    extend: 'portal.layer.renderer.Renderer',

    cityStore : null,

    /**
     * Expects a Ext.util.Observable config with the following additions
     * {
     *  map : An instance of a google map GMap2 object
     * }
     */
    constructor: function(config) {

        this.cityStore = config.cityStore;

        // Call our superclass constructor to complete construction process.
        this.callParent(arguments);
    },

    /**
     * An abstract function for displaying data from a variety of data sources. This function will
     * raise the renderstarted and renderfinished events as appropriate. The effect of multiple calls
     * to this function (ie calling displayData again before renderfinished is raised) is undefined.
     *
     * This function will re-render itself entirely and thus may call removeData() during the normal
     * operation of this function
     *
     * function(portal.csw.OnlineResource[] resources,
     *          portal.layer.filterer.Filterer filterer,
     *          function(portal.layer.renderer.Renderer this, portal.csw.OnlineResource[] resources, portal.layer.filterer.Filterer filterer, bool success) callback
     *
     * returns - void
     *
     * resources - an array of data sources which should be used to render data
     * filterer - A custom filter that can be applied to the specified data sources
     * callback - Will be called when the rendering process is completed and passed an instance of this renderer and the parameters used to call this function
     */
    displayData : function(resources, filterer, callback) {
    	this.primitiveManager.clearPrimitives();
    	
    	//Add the cities to the map
		var markers = [];
		for (var i = 0; i < this.cityStore.getCount(); i++) {
			var city = this.cityStore.getAt(i);
			
			var point = Ext.create('portal.map.Point', {
				latitude : city.get('y'),
				longitude : city.get('x'),
				srs : city.get('crs')
			});
			var icon = Ext.create('portal.map.Icon', {
		        url : 'getCityRatingIcon.do?cityId=' + escape(city.get('cityId')),
		        width : 32,
		        height : 32,
		        anchorOffsetX : 16,
		        anchorOffsetY : 16
			});
			markers.push(this.map.makeMarker(city.get('cityId'), city.get('name'), null, null, this.parentLayer, point, icon ));
		}
		
		this.primitiveManager.addPrimitives(markers);
		callback(this, resources, filterer, true);
    },

    /**
     * An abstract function for aborting the display process. If this function is called any
     * in process rendering should attempt to be halted (if possible). If no rendering is underway
     * then this function should have no effect
     *
     * This function will typically be called immediately prior to a remove with the expectation
     * that any existing data be removed from the map AND no more data be added to the map.
     *
     * function()
     *
     * returns - void
     */
    abortDisplay : function() {
    	
    },

    /**
     * An abstract function for creating a legend that can describe the displayed data. If no
     * such thing exists for this renderer then null should be returned.
     *
     * function(portal.csw.OnlineResource[] resources,
     *          portal.layer.filterer.Filterer filterer)
     *
     * returns - portal.layer.legend.Legend or null
     *
     * resources - (same as displayData) an array of data sources which should be used to render data
     * filterer - (same as displayData) A custom filter that can be applied to the specified data sources
     */
    getLegend : function() {
    	return null;
    },

    /**
     * An abstract function that is called when this layer needs to be permanently removed from the map.
     * In response to this function all rendered information should be removed
     *
     * function()
     *
     * returns - void
     */
    removeData : function() {
    	this.primitiveManager.clearPrimitives();
    }
});